﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.Helpers.IoC;
using ReAnimation.Infrastructure.Services.Interfaces;
using System.IO;
using ReAnimation.Domain.Services;
using ReAnimation.Domain;

namespace ReAnimation.MVP.Presenters
{
    public class AnimationProjectSettingsPresenter: BasePresenter<IAnimationProjectSettingsView>
    {
        private IMainModel _mainModel;
        private IAnimationStoreService _animationStoreService;
        private ILazyInject<INewAnimationDialogView> _newAnimationDialog;
        private ILazyInject<IVideoDialogView> _videoDialog;
        private ILazyInject<ISaveFileDialogView> _saveFileDialogInj;
        private ILazyInject<IOpenFileDialogView> _openFileDialogInj;

        public AnimationProjectSettingsPresenter(IMainModel mainModel,
            IAnimationStoreService animationStoreService,
                                                 ILazyInject<INewAnimationDialogView> newAnimationDialog,
                                                 ILazyInject<IVideoDialogView> videoDialog,
                                                 ILazyInject<ISaveFileDialogView> saveFileDialogInj,
                                                 ILazyInject<IOpenFileDialogView> openFileDialogInj)
        {
            _mainModel = mainModel;
            _animationStoreService = animationStoreService;
            _videoDialog = videoDialog;
            _newAnimationDialog = newAnimationDialog;
            _saveFileDialogInj = saveFileDialogInj;
            _openFileDialogInj = openFileDialogInj;
            
        }

        protected override void InitView()
        {
            _mainModel.CurrentAnimationChanged += new EventHandler(_mainModel_CurrentAnimationChanged);
            _view.NewAnimation += new EventHandler(_view_NewAnimation);
            _view.SaveAnimation += new EventHandler(_view_SaveAnimation);
            _view.LoadAnimation += new EventHandler(_view_LoadAnimation);
            _view.Video += new EventHandler(_view_Video);
            UpdateElementsEnableState();
        }

        void _view_Video(object sender, EventArgs e)
        {
            if (_mainModel.CurrentAnimation.AnimationSteps.Count() != 0)
            {
                _videoDialog.Get().Show();
            }
        }

        void _view_LoadAnimation(object sender, EventArgs e)
        {
            IOpenFileDialogView openFileDialog = _openFileDialogInj.Get();
            if (openFileDialog.ShowOpenFileDialog())
            {
                Animation animation = _animationStoreService.ReadFromFile(openFileDialog.FileName);
                if (animation != null)
                {
                    _mainModel.CurrentAnimation = animation;
                    //TODO: place this logic into model
                    _mainModel.CurrentAnimationStep = _mainModel.CurrentAnimation.AnimationSteps.FirstOrDefault();

                    if (_mainModel.CurrentAnimationStep != null)
                        _mainModel.CurrentMolecule = _mainModel.CurrentAnimationStep.Molecules.FirstOrDefault();
                    else
                        _mainModel.CurrentMolecule = null;
                }
            }
        }

        void _view_SaveAnimation(object sender, EventArgs e)
        {
            if(_mainModel.CurrentAnimation == null) return;
            ISaveFileDialogView saveFileDialog = _saveFileDialogInj.Get();
            if (saveFileDialog.ShowOpenFileDialog())
            {
                _animationStoreService.SaveToFile(_mainModel.CurrentAnimation, saveFileDialog.FileName);
                //TODO: show error...
            }
        }

        void _view_NewAnimation(object sender, EventArgs e)
        {
            _newAnimationDialog.Get().ShowViewDialog();
        }

        void _mainModel_CurrentAnimationChanged(object sender, EventArgs e)
        {
            UpdateElementsEnableState();
        }

        private void UpdateElementsEnableState()
        {
            if (_mainModel.CurrentAnimation != null)
            {
                View.VideoEnabled =
                View.AnimationStepsEnabled = 
                View.AnimationStepSettingsEnabled = 
                View.LoadAnimationEnabled =
                View.SaveAnimationEnabled =
                View.NewAnimationEnabled  = true;
            }
            else
            {
                View.LoadAnimationEnabled =
                View.NewAnimationEnabled = true;

                View.VideoEnabled = 
                View.AnimationStepsEnabled = 
                View.AnimationStepSettingsEnabled = 
                View.SaveAnimationEnabled = false;
            }
        }
    }
}
